package mqtt;

import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class MQTTClient {

    private static final String BROKER_URL = "tcp://mqtt.eclipseprojects.io:1883";
    private static final String CLIENT_ID = "JavaClient";
    private static final String TOPIC = "book666";
    private static String USERNAME ;
    private static String PASSWORD;
    private static final int QOS = 0;
    private final String brokerUrl;
    private final String clientId;
    private final String topic;
    private final String username;
    private final String password;
    private final int qos;
    private MqttClient client;
    private final MqttConnectOptions options = new MqttConnectOptions();

    public MQTTClient() {
        this(BROKER_URL,CLIENT_ID + System.currentTimeMillis(),TOPIC,USERNAME,PASSWORD,QOS);
    }

    public MQTTClient(String brokerUrl, String clientId, String topic) {
        this(brokerUrl,clientId,topic,USERNAME,PASSWORD,QOS);
    }

    public MQTTClient(String brokerUrl, String clientId, String topic, String username, String password, int qos) {
        this.brokerUrl = brokerUrl;
        this.clientId = clientId;
        this.topic = topic;
        this.username = username;
        this.password = password;
        this.qos = qos;
        // 连接参数
        options.setUserName(username);
        if (password != null) {
            options.setPassword(password.toCharArray());
        }
        options.setCleanSession(true);
        options.setConnectionTimeout(60);
        options.setKeepAliveInterval(60);
        try {
            client = new MqttClient(brokerUrl, clientId, new MemoryPersistence());
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    public void connect(MqttCallback callback) {
        try {
            client.setCallback(callback);//设置回调
            client.connect(options);//连接
            client.subscribe(topic,qos);//订阅
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    public void connect() {
        try {
            client.connect(options);//连接
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    // 发布消息
    public void publish(String message) {
        try {
            MqttMessage mqttMessage = new MqttMessage(message.getBytes());
            client.publish(topic, mqttMessage);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    // 关闭连接
    public void disconnect() {
        try {
            client.disconnect();
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    // 关闭客户端
    public void close() {
        try {
            client.close();
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    public String getTopic() {
        return topic;
    }

    public int getQos() {
        return qos;
    }

    public MqttClient getClient() {
        return client;
    }

    public MqttConnectOptions getOptions() {
        return options;
    }
}
